package com.yupi.springbootinit.handler;

import com.yupi.springbootinit.esdao.PostEsDao;
import com.yupi.springbootinit.model.dto.post.PostEsDTO;
import com.yupi.springbootinit.model.entity.Post;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import javax.annotation.Resource;

@Component
@CanalTable(value = "post")
@Slf4j
public class PostHandler implements EntryHandler<Post> {

    // 引入操作 es 的javaApi
    @Resource
    private PostEsDao postEsDao;

    @Override
    public void insert(Post post) {
        log.info("新增:{}", post);
        PostEsDTO postEsDTO = PostEsDTO.objToDto(post);
        postEsDao.save(postEsDTO);
    }

    @Override
    public void update(Post before, Post after) {
        log.info("修改前,行数据为:{}", before);
        log.info("修改后,行数据为:{}", after);
        PostEsDTO postEsDTO = PostEsDTO.objToDto(after);
        postEsDao.save(postEsDTO);
    }

    @Override
    public void delete(Post post) {
        log.info("删除:{}",post);
        PostEsDTO postEsDTO = PostEsDTO.objToDto(post);
        postEsDao.delete(postEsDTO);
    }
}
